﻿using System;
using System.Collections;

using IBatisNet.DataAccess;

using MT.eVotePRS.Model;
using MT.eVotePRS.Service;
using MT.eVotePRS.Persistence.Interfaces;

namespace MT.eVotePRS.Service
{
    public class StudentInfoService : IStudentInfoService
    {
        #region Private Fields
        private static StudentInfoService _instance = new StudentInfoService();
        private IDaoManager _daoManager = null;
        private IStudentInfoDao _studentInfoDao = null;

        #endregion

        #region Constructor

        private StudentInfoService()
        {
            _daoManager = ServiceConfig.GetInstance().DaoManager;
            _studentInfoDao = _daoManager.GetDao(typeof(IStudentInfoDao)) as IStudentInfoDao;

        }

        #endregion

        #region Public methods

        public static StudentInfoService GetInstance()
        {
            return _instance;
        }

        public object InsertStudent(StudentInfo student)
        {
            object obj = null;

            _daoManager.BeginTransaction();

            try
            {
                obj = _studentInfoDao.InsertStudentInfo(student);
                _daoManager.CommitTransaction();
            }
            catch 
            {
                _daoManager.RollBackTransaction();
                throw;
            }

            return obj; 
        }

        public void UpdateStudentInfoById(StudentInfo student)
        {
            _daoManager.BeginTransaction();

            try
            {
                _studentInfoDao.UpdateStudentInfoById(student);
                _daoManager.CommitTransaction();
            }
            catch
            {
                _daoManager.RollBackTransaction();
                throw;
            }
        }

        public StudentInfo GetStudentByBarcode(string barcode)
        {
            StudentInfo student = null;

            _daoManager.OpenConnection();
            student = _studentInfoDao.GetStudentInfoByBarcode(barcode);
            _daoManager.CloseConnection();

            return student;
        }

        public StudentInfo GetStudentByNo(string studentNo)
        {
            StudentInfo student = null;

            _daoManager.OpenConnection();
            student = _studentInfoDao.GetStudentInfoByNo(studentNo);
            _daoManager.CloseConnection();

            return student;
        }

        #endregion
    }
}
